<template>
	<view class="pay-success-container">
		<view>
			<view class="text">支付成功 ￥{{ payInfo.sumAllAmount ? (payInfo.sumAllAmount <= 0 ? 0 : payInfo.sumAllAmount) : 0 }}</view>
			<view style="text-align: center; margin-top: 10rpx; color: #999999; font-size: 24rpx">{{ payInfo.orderDate || 0 }}</view>
			<view class="back-btn">
				<u-button type="primary" @click="goOrder">返回订单</u-button>
			</view>
		</view>
		<uni-popup type="bottom" ref="couponRef" :mask-click="false">
			<view class="cunpon-box">
				<view class="price">
					<text class="success">支付成功</text>
					<text class="amount">
						<text style="font-size: 38rpx">￥</text>
						{{ payInfo.sumAllAmount }}
					</text>
				</view>
				<view class="pay-method">
					{{ payInfo.paymentName || '' }}
				</view>

				<view class="coupon-box">
					<view class="coupon-item" v-for="(item, index) in couponList" :key="item.id">
						<view class="left">
							<view>
								{{ item.typeName }}
							</view>
						</view>
						<view class="right">
							<view class="coupon-info">
								<view class="name">
									{{ item.name }}
									<text style="margin-left: 10rpx; font-size: 26rpx; color: #999999">×{{ item.couponCount }}</text>
								</view>
								<view class="date">
									{{ item.validityType === 1 ? '' : item.startDate.slice(0, 10) + ' - ' }}
									{{ item.validityType === 1 ? item.validityDesc : item.endDate.slice(0, 10) }}
								</view>
							</view>
							<view :style="{ background: item.isDraw ? '#f7f7f7' : 'linear-gradient(34deg, #F3C45D, #EDAC2F)' }" class="confirm-btn" @click="drawBtn(item, index)">
								{{ item.isDraw ? '已' : '立即' }}领取
							</view>
						</view>
					</view>
				</view>
				<view style="padding: 54rpx;30rpx; 30rpx">
					<view class="success-btn" @click="onclose">完成</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
export default {
	name: 'pay_success',
	data() {
		return {
			couponList: [],
			payInfo: {}
		};
	},
	async onLoad({ orderId }) {
		await this.getList(orderId);
		this.$nextTick(() => {
			this.$refs.couponRef.open();
		});
	},
	methods: {
		async getList(orderId) {
			const res = await this.$api.getRCosumerList({
				id: orderId
			});
			this.couponList = res.data.couponList || [];
			this.couponList =
				this.couponList && this.couponList.length > 0
					? this.couponList.forEach((item) => {
							this.$set(item, 'isDraw', false);
					  })
					: [];
			this.payInfo = res.data.orderMaster;
		},
		goOrder() {
			uni.switchTab({
				url: '/pages/tab_bar/order/order'
			});
		},
		async drawBtn(item, index) {
			if (item.isDraw) return;
			await this.$api.ReceivePommotion({
				id: item.id,
				promotionId: this.payInfo.promotionId,
				grantId: this.payInfo.grantId,
				promotionType: this.payInfo.promotionType,
				count: item.couponCount,
				changeStock: this.payInfo.changeStock
			});
			this.couponList[index].isDraw = true;
		},
		onclose() {
			this.$refs.couponRef.close();
		}
	}
};
</script>

<style lang="scss">
view {
	box-sizing: border-box;
}

.pay-success-container {
	.text {
		font-size: 40rpx;
		color: #333;
		text-align: center;
		padding-top: 50rpx;
	}

	.back-btn {
		width: 50%;
		margin: 40rpx auto;
	}

	.cunpon-box {
		width: 750rpx;
		background-color: #fff;
		min-height: 600rpx;
		border-radius: 30rpx 30rpx 0rpx 0rpx;
		padding-top: 70rpx;

		.price {
			text-align: center;
			vertical-align: bottom;
			color: #111111;

			.success {
				font-weight: bold;
				font-size: 36rpx;
				line-height: 55rpx;
				margin-right: 8rpx;
			}

			.amount {
				font-weight: 800;
				font-size: 40rpx;
				line-height: 55rpx;
			}
		}

		.pay-method {
			font-weight: 500;
			font-size: 24rpx;
			color: #666666;
			line-height: 55rpx;
			margin-top: 20rpx;
			text-align: center;
		}

		.coupon-box {
			margin-top: 40rpx;
			padding: 0 30rpx;

			.coupon-item {
				display: flex;
				margin-top: 20rpx;
				width: 690rpx;
				height: 190rpx;

				.left {
					width: 180rpx;
					padding: 62rpx 0;
					border-top: 8rpx solid #edac2f;
					border-left: 8rpx solid #edac2f;
					border-bottom: 8rpx solid #edac2f;
					border-radius: 16rpx;

					& > view {
						text-align: center;
						height: 42rpx;
						font-weight: 600;
						font-size: 35rpx;
						color: #111111;
						line-height: 47rpx;
					}
				}

				.right {
					display: flex;
					align-items: center;
					flex: 1;
					border-top: 8rpx solid #edac2f;
					border-right: 8rpx solid #edac2f;
					border-bottom: 8rpx solid #edac2f;
					border-left: 2rpx dashed #edac2f;
					border-radius: 16rpx;

					.coupon-info {
						flex: 1;
						padding-left: 19rpx;

						.name {
							font-weight: bold;
							font-size: 30rpx;
							color: #111111;
							line-height: 42rpx;
						}

						.date {
							font-weight: 500;
							font-size: 22rpx;
							color: #999999;
							line-height: 42rpx;
							margin-top: 16rpx;
						}
					}

					.confirm-btn {
						width: 140rpx;
						height: 56rpx;
						border-radius: 28rpx;
						margin-right: 20rpx;
						text-align: center;
						line-height: 56rpx;
						font-weight: 500;
						font-size: 22rpx;
						color: #111111;
					}
				}
			}
		}

		.success-btn {
			height: 90rpx;
			background: linear-gradient(34deg, #f3c45d, #edac2f);
			border-radius: 45rpx;
			font-weight: bold;
			font-size: 30rpx;
			color: #111111;
			line-height: 90rpx;
			text-align: center;
		}
	}
}
</style>
